<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>cs229 第一节 | Dirac Lee</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noodp" />
<meta name="Description" content="cs229 第一节"><link rel="prev" href="https://diraclee.gitee.io/2020/06/install_golong/" /><link rel="next" href="https://diraclee.gitee.io/2020/07/design_pattern/" /><link rel="canonical" href="https://diraclee.gitee.io/2020/06/cs229_lecture1/" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff"><meta property="og:title" content="cs229 第一节" />
<meta property="og:description" content="cs229 第一节" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://diraclee.gitee.io/2020/06/cs229_lecture1/" />
<meta property="article:published_time" content="2020-06-29T00:00:00+00:00" />
<meta property="article:modified_time" content="2020-07-19T00:00:00+00:00" />
<script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "cs229 第一节",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/diraclee.gitee.io\/2020\/06\/cs229_lecture1\/"
        },"image": {
                "@type": "ImageObject",
                "url": "https:\/\/diraclee.gitee.io\/logo.png",
                "width":  800 ,
                "height":  600 
            },"genre": "posts","keywords": "机器学习, cs229","wordcount":  740 ,
        "url": "https:\/\/diraclee.gitee.io\/2020\/06\/cs229_lecture1\/","datePublished": "2020-06-29T00:00:00\x2b00:00","dateModified": "2020-07-19T00:00:00\x2b00:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
                "@type": "Organization",
                "name": "xxxx",
                "logo": {
                "@type": "ImageObject",
                "url": "https:\/\/diraclee.gitee.io\/logo.png",
                "width":  127 ,
                "height":  40 
                }
            },"description": "cs229 第一节"
    }
    </script><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/css/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/css/lib/animate/animate.min.css"></head>
    <body><script>
            window.isDark = (window.localStorage && window.localStorage.getItem('theme')) === 'dark';
            window.isDark && document.body.classList.add('dark-theme');
        </script><div class="wrapper"><nav class="navbar">
    <div class="navbar-container">
        <div class="navbar-header animated bounceIn">
            <a href="https://diraclee.gitee.io">Dirac Lee</a>
        </div>
        <div class="navbar-menu"><a class="menu-item" href="https://diraclee.gitee.io/posts" title="">文章</a><a class="menu-item" href="https://diraclee.gitee.io/tags" title="">标签</a><a class="menu-item" href="https://diraclee.gitee.io/categories" title="">分类</a><a class="menu-item" href="https://diraclee.gitee.io/about" title="">关于</a><a class="menu-item" href="https://diraclee.gitee.io" title="English"><i class="fas fa-language fa-fw"></i></a><a href="javascript:void(0);" class="theme-switch"><i class="fas fa-adjust fa-rotate-180 fa-fw" title="切换主题"></i></a>
        </div>
    </div>
</nav><nav class="navbar-mobile">
    <div class="navbar-container">
        <div class="navbar-header">
            <div class="navbar-header-title animated bounceIn">
                <a href="https://diraclee.gitee.io">Dirac Lee</a>
            </div>
            <div class="menu-toggle" id="menu-toggle">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="navbar-menu" id="mobile-menu"><a class="menu-item" href="https://diraclee.gitee.io/posts" title="">文章</a><a class="menu-item" href="https://diraclee.gitee.io/tags" title="">标签</a><a class="menu-item" href="https://diraclee.gitee.io/categories" title="">分类</a><a class="menu-item" href="https://diraclee.gitee.io/about" title="">关于</a><a class="menu-item" href="https://diraclee.gitee.io" title="English"></a><a href="javascript:void(0);" class="theme-switch"><i class="fas fa-adjust fa-rotate-180 fa-fw" title="切换主题"></i></a>
        </div>
    </div>
</nav><main class="main">
                <div class="container"><article class="page"><h1 class="post-title animated flipInX">cs229 第一节</h1><div class="post-meta">
            <div class="post-meta-main"><a class="author" href="https://diraclee.gitee.io" rel="author" target="_blank">
                    <i class="fas fa-user-circle fa-fw"></i>Dirac Lee
                </a>&nbsp;<span class="post-category">收录于&nbsp;<i class="far fa-folder fa-fw"></i><a href="https://diraclee.gitee.io/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a>&nbsp;</span></div>
            <div class="post-meta-other"><i class="far fa-calendar-alt fa-fw"></i><time datetime=2020-06-29>2020-06-29</time>&nbsp;
                <i class="fas fa-pencil-alt fa-fw"></i>约 740 字&nbsp;
                <i class="far fa-clock fa-fw"></i>预计阅读 2 分钟&nbsp;</div>
        </div><div class="post-content"><ul>
<li>线性回归</li>
<li>批量梯度下降与随机梯度下降</li>
<li>正规方程</li>
</ul>
<a class="post-dummy-target" id="监督学习"></a><h3>监督学习</h3>
<a class="post-dummy-target" id="回归问题"></a><h4>回归问题</h4>
<table>
<thead>
<tr>
<th>大小(feet<sup>2</sup>)​</th>
<th>卧室数量</th>
<th>价格($1000s)​</th>
</tr>
</thead>
<tbody>
<tr>
<td>2614</td>
<td>3</td>
<td>400</td>
</tr>
<tr>
<td>1416</td>
<td>3</td>
<td>232</td>
</tr>
<tr>
<td>1534</td>
<td>3</td>
<td>315</td>
</tr>
<tr>
<td>852</td>
<td>2</td>
<td>178</td>
</tr>
<tr>
<td>&hellip;</td>
<td>&hellip;</td>
<td>&hellip;</td>
</tr>
</tbody>
</table>
<p><figure><img src="/svg/loading.min.svg" data-sizes="auto" data-src="https://gitee.com/DiracLee/picbed/raw/master/img/20200718220840.png" alt="" class="lazyload"></figure></p>
<p>$h(x) = \theta_0 + \theta_1 x_1 + \theta_2 x_2$</p>
<p>$h(x) = \sum_{j=0}^2 \theta_j x_j  \quad$ where $x_0 = 1$</p>
<table>
<thead>
<tr>
<th align="left">标记</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">n</td>
<td>特征数量</td>
</tr>
<tr>
<td align="left">m</td>
<td>训练样本数量</td>
</tr>
<tr>
<td align="left">$\theta$</td>
<td>参数</td>
</tr>
<tr>
<td align="left">x</td>
<td>输入 / 特征</td>
</tr>
<tr>
<td align="left">y</td>
<td>输出 / 目标标签</td>
</tr>
<tr>
<td align="left">(x, y)</td>
<td>训练样本</td>
</tr>
<tr>
<td align="left">(x<sup>(i)</sup>, y<sup>(i)</sup>)</td>
<td>第 i 个训练样本</td>
</tr>
</tbody>
</table>
<p>选择 $\theta$ 使得 $h_\theta(x) \approx y$ 对每一个训练样本都成立</p>
<p>$J(\theta) = \frac 1 2 \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)})^2$<br>
$\theta^* = \arg \min_\theta  J(\theta)$</p>
<a class="post-dummy-target" id="批量梯度下降"></a><h3>批量梯度下降</h3>
<ul>
<li>
<p>为 $\theta$ 进行初始化 (比如令 $\theta = \vec 0$)</p>
</li>
<li>
<p>不断改变 $\theta$ 的值来降低 $J(\theta)$</p>
<p>$$
\begin{aligned}
\theta_j 
&amp;:= \theta_j - \alpha \frac \partial {\partial \theta_j} J(\theta)  \\ 
&amp;= \theta_j - \alpha \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)}) x_j^{(i)}
\end{aligned}
$$</p>
</li>
<li>
<p>重复以上步骤直到收敛，即 $J(\theta)$ 不再下降</p>
</li>
</ul>
<p>$J(\theta)$ 的梯度推导:</p>
<p>$$
\begin{aligned}
\frac \partial {\partial \theta_j} J(\theta) 
&amp;= \frac \partial {\partial \theta_j} \frac 1 2 \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)})^2 \\ 
&amp;=  \frac 1 2 \times 2 \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)}) \frac \partial {\partial \theta_j} (h_\theta(x^{(i)}) - y^{(i)}) \\ 
&amp;= \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)}) \frac \partial {\partial \theta_j} (\theta_0 x_0^{(i)} + \theta_1 x_1^{(i)} + &hellip; + \theta_n x_n^{(i)}  - y^{(i)}) \\ 
&amp;= \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)}) x_j^{(i)}
\end{aligned}
$$</p>
<blockquote>
<p>对于大样本集, 批量梯度下降代价过于昂贵。因为它每迭代一次，就需要遍历整个数据集。</p>
</blockquote>
<a class="post-dummy-target" id="随机梯度下降"></a><h3>随机梯度下降</h3>
<ul>
<li>
<p>为 $\theta$ 进行初始化 (比如令 $\theta = \vec 0$)</p>
</li>
<li>
<p>不断改变 $\theta$ 的值来降低 $J(\theta)$</p>
<p>$$
\theta_j := \theta_j - \alpha (h_\theta(x^{(i)}) - y^{(i)}) x^{(i)}_j
$$</p>
</li>
<li>
<p>重复以上步骤直到收敛，即 $J(\theta)$ 不再下降</p>
</li>
</ul>
<blockquote>
<p>随机梯度下降算法不保证每一次参数更新都能更靠近全局最优解, 但平均来看，它的整体方向是向全局最优解逼近的。</p>
</blockquote>
<a class="post-dummy-target" id="正则方程"></a><h3>正则方程</h3>
<a class="post-dummy-target" id="结论"></a><h4>结论</h4>
<p>如果记</p>
<p>$$
X = 
\begin{bmatrix}
x^{(1)}_0 &amp; x^{(1)}_1 &amp; \dots &amp; x^{(1)}_n \\ 
\\ 
x^{(2)}_0 &amp; x^{(2)}_1 &amp; \dots &amp; x^{(2)}_n \\ 
\\ 
\vdots    &amp; \vdots    &amp; \dots &amp; \vdots    \\ 
\\ 
x^{(m)}_0 &amp; x^{(m)}_1 &amp; \dots &amp; x^{(m)}_n \\ 
\end{bmatrix}
$$</p>
<p>$$
\theta = 
\begin{bmatrix}
\theta_0 \\ 
\theta_1 \\ 
\vdots    \\ 
\theta_n \\ 
\end{bmatrix}
$$</p>
<p>那么我们就能得到</p>
<p>$$
X^TX \theta = X^T y
$$</p>
<p>特别地，如果矩阵 $X^T X$ 可逆, 那么上式等价于</p>
<p>$$
\theta = (X^TX )^{-1} X^T y
$$</p>
<a class="post-dummy-target" id="推导"></a><h4>推导</h4>
<p>已知
$$
\begin{aligned}
J(\theta) 
&amp;= \frac 1 2 \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)})^2 \\ 
&amp;= \frac 1 2 (X \theta - y)^T (X \theta - y)
\end{aligned}
$$</p>
<p>那么</p>
<p>$$
\begin{aligned}
\nabla_\theta J(\theta)
&amp;= \nabla_\theta \frac 1 2 (X \theta - y)^T (X \theta - y) \\ 
&amp;= \frac 1 2 \nabla_\theta (X \theta - y)^T (X \theta - y) \\ 
&amp;= \frac 1 2 \nabla_\theta (\theta^T X^T X \theta - \theta^T X^T y - y^T X \theta + y^T y) \\ 
&amp;= \frac 1 2 (X^T X \theta + X^T X \theta - X^T y - X^T y) \\ 
&amp;= X^T X \theta - X^T y
\end{aligned}
$$</p>
<p>令 $\nabla_\theta J(\theta) = 0$，从而得解。</p>
<a class="post-dummy-target" id="nabla-算子"></a><h4>Nabla 算子</h4>
<p>$$
\nabla_\theta J(\theta) = 
\begin{bmatrix}
\frac {\partial } {\partial \theta_0} J(\theta) \\ 
\\ 
\frac {\partial } {\partial \theta_1} J(\theta) \\ 
\\ 
\vdots \\ 
\\ 
\frac {\partial } {\partial \theta_n} J(\theta)
\end{bmatrix}
$$</p>
<a class="post-dummy-target" id="参考"></a><h2>参考</h2>
<p><a href="https://katex.org/docs/supported.html" target="_blank">$\KaTeX$ 文档</a></p></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>本文于 2020-07-19 更新</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share"><span><a href="//www.linkedin.com/shareArticle?url=https%3a%2f%2fdiraclee.gitee.io%2f2020%2f06%2fcs229_lecture1%2f&amp;title=cs229%20%e7%ac%ac%e4%b8%80%e8%8a%82" target="_blank" title="分享到 LinkedIn">
            <i class="fab fa-linkedin fa-fw"></i>
        </a><a href="//service.weibo.com/share/share.php?url=https%3a%2f%2fdiraclee.gitee.io%2f2020%2f06%2fcs229_lecture1%2f&amp;appkey=&amp;title=cs229%20%e7%ac%ac%e4%b8%80%e8%8a%82" target="_blank" title="分享到 Weibo">
            <i class="fab fa-weibo fa-fw"></i>
        </a></span></div>
        </div>
    </div>

    <div class="post-info-more">
        <section><span class="tag">
                        <a href="https://diraclee.gitee.io/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/"><i class="fas fa-tag fa-fw"></i>&nbsp;机器学习</a>&nbsp;
                    </span><span class="tag">
                        <a href="https://diraclee.gitee.io/tags/cs229/"><i class="fas fa-tag fa-fw"></i>&nbsp;cs229</a>&nbsp;
                    </span></section>
        <section>
            <span><a href="javascript:window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="https://diraclee.gitee.io">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="https://diraclee.gitee.io/2020/06/install_golong/" class="prev" rel="prev" title="Golang 的安装与配置"><i class="fas fa-angle-left fa-fw"></i>Golang 的安装与配置</a>
            <a href="https://diraclee.gitee.io/2020/07/design_pattern/" class="next" rel="next" title="设计模式">设计模式<i class="fas fa-angle-right fa-fw"></i></a></div>
</div><div class="post-comment"></div>
    </article></div>
            </main><footer class="footer">
    <div class="copyright"><div class="copyright-line">由 <a href="https://gohugo.io/" target="_blank" rel="external nofollow noopener noreffer">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="external nofollow noopener noreffer">LoveIt<i class="far fa-heart fa-fw"></i></a>
        </div>

        <div class="copyright-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2020</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="https://diraclee.gitee.io" target="_blank">Dirac Lee</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
    </div>
</footer></div><a href="#" class="dynamic-to-top" id="dynamic-to-top" data-scroll>
            <span>&nbsp;</span>
        </a><script src="/js/lib/jquery/jquery.slim.min.js"></script><script src="/js/lib/lazysizes/lazysizes.min.js"></script><script src="/js/lib/smooth-scroll/smooth-scroll.polyfills.min.js"></script><script>window.scroll = new SmoothScroll('[data-scroll]', {speed: 300, speedAsDuration: true});</script><link rel="stylesheet" href="/css/lib/katex/katex.min.css"><script src="/js/lib/katex/katex.min.js"></script><script defer src="/js/lib/katex/auto-render.min.js"></script><link rel="stylesheet" href="/css/lib/katex/copy-tex.min.css"><script defer src="/js/lib/katex/copy-tex.min.js"></script><script defer src="/js/lib/katex/mhchem.min.js"></script><script>
        document.addEventListener("DOMContentLoaded", function () {
            renderMathInElement(document.body, {
                delimiters: [
                    { left: "$$", right: "$$", display: true },
                    { left: "\\(", right: "\\)", display: false },
                    { left: "\\[", right: "\\]", display: true },{ left: "$", right: "$", display: false },]
            });
        });
    </script><script src="/js/blog.min.js"></script></body>
</html>